iT邦幫忙

2021 iThome 鐵人賽

DAY 28
0
Mobile Development

ios 的小小實驗室 !系列 第 28

DAY 28『 使用相機拍照 』 ImagePicker - Part2

  • 分享至 

  • xImage
  •  

昨天的實作練習,有相機,會執行以下的程式碼:

  1. 令 vc 為 UIImagePickerController
    let vc = UIImagePickerController()
  2. 新增相機權限
    vc.sourceType = .camera // 相簿
  3. 設定委任
    vc.delegate = self
  4. 允許用戶編輯選定的圖像或影像
    vc.allowsEditing = true
  5. 設定跳頁
    present(vc, animated: true)

到這邊會報錯是正常的,因為沒給它 UIImagePickerControllerDelegate
因此在專案裡用 extension 去寫委任

extension MainVC: UIImagePickerControllerDelegate, UINavigationControllerDelegate{
    func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
        // 顯示 photo
        guard let image = info[UIImagePickerController.InfoKey.originalImage] as? UIImage else {return}
        imageView.image = image
        picker.dismiss(animated: true, completion: nil)
    }
    func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
        picker.dismiss(animated: true, completion: nil)
    }
}

附上完整程式碼

import UIKit

class MainVC: UIViewController {

    @IBOutlet weak var imageView: UIImageView!
    @IBOutlet weak var takePhoto: UIButton!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.navigationController?.setNavigationBarHidden(true, animated: false) // 隱藏指示條
    }
    
    @IBAction func didTapButton(btn: UIButton) {
        let vc = UIImagePickerController()
        if !UIImagePickerController.isSourceTypeAvailable(.camera) {
            let alertController = UIAlertController(title: "ERROR", message: "Device has no camera.", preferredStyle: .alert)
            let okAction = UIAlertAction(title: "Alright", style: .default, handler: { (alert: UIAlertAction!) in
            })
            alertController.addAction(okAction)
            self.present(alertController, animated: true, completion: nil)
            print("ERROR:沒有相機功能!")
        }
        else { vc.sourceType = .camera }
        vc.delegate = self
        present(vc, animated: true)
    }
}

extension MainVC: UIImagePickerControllerDelegate, UINavigationControllerDelegate{
    func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
        picker.dismiss(animated: true, completion: nil)
        // 顯示 photo
        guard let image = info[UIImagePickerController.InfoKey.originalImage] as? UIImage else {return}
        imageView.image = image
    }
    func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
        picker.dismiss(animated: true, completion: nil)
    }
}

為了測試和使用相機,您必須使用真正的連接設備,若燒在模擬器,會報錯:Source type 1 not available


這樣就完成從相簿選取照片的實作了!明天會分享從相簿選取照片( 有裁剪照片功能 ),敬請期待!


上一篇
DAY 27『 使用相機拍照 』 ImagePicker - Part1
下一篇
DAY 29『 從相簿選取照片( 有裁剪照片功能 ) 』ImagePicker - Part1
系列文
ios 的小小實驗室 !30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言